const API_BASE = "http://192.168.31.104:8088";
// const API_BASE = "http://144.255.19.26:8088";
const API_URL = API_BASE + "/index.php/EditorApi";

export function API_BaseUrl() {
    return API_BASE;
}

export function API_Url() {
    return API_URL;
}

/** 登录提交 */
export function API_LoginPost(user, pswd, callback = null) {
    HttpRequest.post(API_URL + "/login", {
        username: user,
        password: pswd,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

/** 注册提交 */
export function API_RegisterPost(user, phone, pswd, callback = null) {
    HttpRequest.post(API_URL + "/register", {
        username: user,
        phone: phone,
        password: pswd,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetEditorInfo(token, callback = null) {
    HttpRequest.post(API_URL + "/getEditorInfo", {
        token: token,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

/** 新建工程 */
export function API_NewProject(token, name, describe, callback = null) {
    HttpRequest.post(API_URL + "/newProject", {
        token: token,
        name: name,
        describe: describe,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetFile(token, $fileid, callback = null) {
    HttpRequest.post(API_URL + "/getFile", {
        token: token,
        id: $fileid,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_SaveXMLFile(token, $fileid, $content, callback = null) {
    HttpRequest.post(API_URL + "/saveXMLFile", {
        token: token,
        id: $fileid,
        content: $content,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetProjectList(token, callback = null) {
    HttpRequest.post(API_URL + "/getProjectList", {
        token: token,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_DeleteProject(token, projectId, callback = null) {
    HttpRequest.post(API_URL + "/deleteProject", {
        token: token,
        projectId: projectId,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetProjectInfo(token, projectId, callback = null) {
    HttpRequest.post(API_URL + "/getProjectInfo", {
        token: token,
        project_id: projectId,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetProjectFile(token, projectId, callback = null) {
    HttpRequest.post(API_URL + "/getProjectFile", {
        token: token,
        project_id: projectId,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_UploadImages(token, projectId, files, callback = null) {
    const formData = new FormData();
    formData.append("token", token);
    formData.append("project_id", projectId);
    // 添加所有文件
    files.forEach((file, index) => {
        formData.append(`images[${index}]`, file);
    });
    // 创建XMLHttpRequest对象
    const xhr = new XMLHttpRequest();
    // 上传进度事件
    xhr.upload.addEventListener("progress", function (e) {
        if (e.lengthComputable) {
            const percentComplete = Math.round((e.loaded / e.total) * 100);
            // progressBar.value = percentComplete;
            // progressText.textContent = `${percentComplete}%`;
            console.log(`上传进度: ${percentComplete}%`);
        }
    });
    // 请求完成事件
    xhr.addEventListener("load", function () {
        if (xhr.status === 200) {
            const response = JSON.parse(xhr.responseText);
            // showUploadResults(response);
            // console.log("上传成功:", response);
            if (callback) {
                callback(response);
            }
        } else {
            console.log("上传失败:", xhr.statusText);
        }
    });

    // 错误事件
    xhr.addEventListener("error", function () {
        console.log("上传发生错误");
    });

    // 配置请求
    xhr.open("POST", API_URL + "/uploadImages", true);

    // 发送请求
    xhr.send(formData);
}

export function API_GetImageUrl(token, fileId) {
    return API_URL + "/getFile?token=" + token + "&id=" + fileId;
}

export function API_DeleteFile(token, fileId, callback = null) {
    HttpRequest.post(API_URL + "/deleteFile", {
        token: token,
        id: fileId,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_RenameFile(token, fileId, name, callback = null) {
    HttpRequest.post(API_URL + "/renameFile", {
        token: token,
        id: fileId,
        name: name,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_GetProjectUrl(token, projectId) {
    return (
        API_URL + "/image?token=" + token + "&projectId=" + projectId + "&url="
    );
}

export function API_ExportProject(token, projectId, callback = null) {
    HttpRequest.post(API_URL + "/exportProject", {
        token: token,
        project_id: projectId,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}

export function API_CreateView(token, projectId, name, callback = null) {
    HttpRequest.post(API_URL + "/createView", {
        token: token,
        projectId: projectId,
        name: name,
    }).then((res) => {
        if (callback) {
            callback(res);
        }
    });
}
